package cn.by.wms.service;

import cn.by.wms.Util.AliOssUtil;
import cn.by.wms.dto.FileInfoDTO;
import cn.by.wms.dto.UploadDTO;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.smart.core.exception.BizException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@Service
public class UploadService {

    @Value("${upload.path}")
    private String path;

    @Value("${upload.url}")
    private String url;
    @Value("${upload.upUrl}")
    private String upUrl;

    public String upload(UploadDTO uploadDTO) {
        String fileName = uploadDTO.getName();
        String fileBase64 = uploadDTO.getFileBase64();
        //分隔开
        String[] Base64Array = StrUtil.splitToArray(fileBase64, "base64,");
        if (Base64Array.length == 0) {
            BizException.throwBizException(416, "base64未识别，请重新上传");
        }
        //解析出base64
        byte[] decode = Base64.decode(Base64Array[1]);
        //设置上传的文件目录和名称
        String name = upUrl + DateUtil.currentSeconds() + "_" + fileName;
        //获取输入流
        ByteArrayInputStream inputStream = new ByteArrayInputStream(decode);
        //上传文件
        AliOssUtil.uploadFile(name, inputStream);
        System.out.println("上传成功");
        //获取URl
        URL url = AliOssUtil.getUrl(name, 60);
        return url.toString();
//        //获取当前时间戳
//        long timestampInSeconds = DateUtil.currentSeconds();
//        //写入内存
//        FileUtil.writeBytes(decode, path + timestampInSeconds +"_"+ name);
//        return url + timestampInSeconds +"_"+  name;


    }

    private final List<String> suffixList = CollUtil.newArrayList("jpg", "png", "jpeg");

    public List<FileInfoDTO> selectImg() {
        List<FileInfoDTO> fileInfoDTOS = new ArrayList<>();
        //拿到路径下的所有文件
        List<String> listPathObjects = AliOssUtil.listPathObjects(upUrl);
        listPathObjects.forEach(path -> {
            String suffix = StrUtil.subAfter(path, ".", true);
            if (suffixList.contains(suffix)) {
                FileInfoDTO fileInfoDTO = new FileInfoDTO();
                String fileName = StrUtil.subAfter(path, "_", true);
                fileInfoDTO.setName(fileName);
                fileInfoDTO.setUrl(AliOssUtil.getUrl(path, 60).toString());
                fileInfoDTOS.add(fileInfoDTO);
            }
        });
//        File[] files = FileUtil.ls(path);
//        //便利，拿到数组
//        Arrays.stream(files).forEach(file -> {
//            if (FileUtil.isFile(file)) {
//                String suffix = FileUtil.getSuffix(file);
//                if (suffixList.contains(suffix)) {
//                    FileInfoDTO fileInfoDTO = new FileInfoDTO();
//                    String fileName = StrUtil.subAfter(file.getName(), "_", true);
//                    fileInfoDTO.setName(fileName);
//                    fileInfoDTO.setUrl(url + file.getName());
//                    fileInfoDTOS.add(fileInfoDTO);
//                }
//            }
//        });
        return fileInfoDTOS;
    }
}
